﻿using SideMan.Views;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace SideMan.Models
{
    public class NoteViewModel : ViewModelBase
    {
        #region Properties
        private SolidColorBrush flagColorBrush;
        public SolidColorBrush FlagColorBrush { get => flagColorBrush; set => SetProperty(ref flagColorBrush, value); }
        private string title;
        public string Title
        {
            get => title;
            set => SetProperty(ref title, value);
        }
        public FlowDocument Document { get; } = new FlowDocument();
        public Action<InlineUIContainer> OnInsertAction { get; set; }
        private string _id;
        public string ID { get => _id; set => SetProperty(ref _id, value); }
        private int _no;
        public int No { get => _no; set => SetProperty(ref _no, value); }
        private string _richTextBoxContent;
        public string RichTextBoxContent { get => _richTextBoxContent; set => SetProperty(ref _richTextBoxContent, value); }
        private int _version;
        public int Version { get => _version; set => SetProperty(ref _version, value); }
        #endregion
        #region Events
        public event EventHandler saveEventHandler;
        #endregion

        // Define commands for toolbar buttons
        //public RelayCommand EditCommand { get; set; }
        //public RelayCommand DeleteCommand { get; set; }
        //public RelayCommand InsertBeforeCommand { get; set; }
        //public RelayCommand InsertAfterCommand { get; set; }
        //public RelayCommand ViewCommand { get; set; }

        public NoteViewModel()
        {
            //EditCommand = new RelayCommand(Edit);
            //DeleteCommand = new RelayCommand(Delete);
            //InsertBeforeCommand = new RelayCommand(InsertBefore);
            //InsertAfterCommand = new RelayCommand(InsertAfter);
            //ViewCommand = new RelayCommand(View);
            FlagColorBrush = new SolidColorBrush { Color = ColorGenerator.GenerateRandomColor() };
            // Initialize the FlowDocument or load it from somewhere
            // For simplicity, this example initializes with some text
            RichTextBoxContent = "";// new FlowDocument(new Paragraph(new Run("RichTextContent")));
            Version = 1;
        }

        private void Edit()
        {
            // Implement edit logic
        }

        private void Delete()
        {
            // Implement delete logic
        }

        private void InsertBefore()
        {
            // Implement insert before logic
        }

        private void InsertAfter()
        {
            // Implement insert after logic
        }

        private void View()
        {
            // Implement view logic
        }
        private void pasteClicked(object obj)
        {
            if (Clipboard.ContainsFileDropList())
            {
                var files = Clipboard.GetFileDropList();
                foreach (string file in files)
                {
                    if (IsImageFile(file))
                    {
                        InsertImage(file);
                    }
                    else
                    {
                        InsertText(File.ReadAllText(file));
                    }
                }
            }
            else if (Clipboard.ContainsText())
            {
                InsertText(Clipboard.GetText());
            }
        }
        private bool IsImageFile(string fileName)
        {
            string extension = Path.GetExtension(fileName)?.ToLower();
            return extension == ".png" || extension == ".jpg" || extension == ".jpeg" || extension == ".gif" || extension == ".bmp";
        }

        private void InsertImage(string imagePath)
        {
            try
            {
                BitmapImage bitmap = new BitmapImage(new Uri(imagePath));
                Image image = new Image { Source = bitmap, Width = 100, Height = 100 };

                InlineUIContainer container = new InlineUIContainer(image);
                OnInsertAction?.Invoke(container);
            }
            catch (Exception ex)
            {
                // Handle image insertion error
            }
        }

        private void InsertText(string text)
        {
            TextRange textRange = new TextRange(Document.ContentEnd, Document.ContentEnd);
            textRange.Text = text;
        }
        private void dragoverClicked(object obj)
        {
            Log.Debug("drag over clicked");
        }
        private void dropClicked(object obj)
        {
            Log.Debug("drop clicked");
        }
        private void openClicked(object obj)
        {
 
        }
        private void closeClicked(object obj)
        {
            saveEventHandler?.Invoke(this, new NoteEventArgs { NoteViewModel = this });
            saveEventHandler = null;
        }
        public void Window_Closed(object sender, EventArgs e)
        {
            closeClicked(null);
        }
        #region Commands
        private RelayCommand<object> _pasteCommand, _dragoverCommand, _dropCommand, _openCommand, _closeCommand;
        public RelayCommand<object> PasteCommand => _pasteCommand ?? (_pasteCommand = new RelayCommand<object>(pasteClicked));
        public RelayCommand<object> DragOverCommand => _dragoverCommand ?? (_dragoverCommand = new RelayCommand<object>(dragoverClicked));
        public RelayCommand<object> DropCommand => _dropCommand ?? (_dropCommand = new RelayCommand<object>(dropClicked));
        public RelayCommand<object> OpenCommand => _openCommand ?? (_openCommand = new RelayCommand<object>(openClicked));
        public RelayCommand<object> CloseCommand => _closeCommand ?? (_closeCommand = new RelayCommand<object>(closeClicked));
        #endregion
    }
}
